package dao.impl;

import dao.Ticket_openDao;
import entry.Ticket_open;
import pojo.XianShi;
import util.DBHelper;
import util.Page;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static utils.PojoUtils.pojo;
import static utils.PojoUtils.pojo1;
@SuppressWarnings("all")
public class Ticket_openDapImpl implements Ticket_openDao {
    private DBHelper db = new DBHelper();

    /**
     * 向开单列表中插入开单信息
     *
     * @param tko 开单列表实体对象
     * @return 通过if判断是否插入成功返回true或false
     */
    @Override
    public boolean insertTicket_open(Ticket_open tko) {
        String sql = "insert into ticket_open values(null,?,?,?,?,?,current_date(),?,?,?,?)";
        int i = db.updateTable(sql, tko.getNo(), tko.getEnterprise_id(), tko.getAcquire_enterprise_id(),
                tko.getAmount(), tko.getInstituty_id(), tko.getExpiry_time(), tko.getPayment_interest_type(), tko.getStatus(),
                tko.getUplink_address());
        return i > 0 ? true : false;
    }

    /**
     * 修改开单表（主要是为了撤销时修改票据状态的）
     *
     * @param tko 开单列表实体对象
     * @return 通过if判断是否修改成功返回true或false
     *///current_date()
    @Override
    public boolean updateTicket_open(Ticket_open tko) {
        String sql = "update ticket_open set status=? where no = ?";
        int i = db.updateTable(sql, tko.getStatus(), tko.getNo());
        return i > 0 ? true : false;
    }

    /**
     * 查询开单表中的所有信息
     *
     * @return 返回开单表集合
     */
    @Override
    public ArrayList<Ticket_open> queryAll(Page page, XianShi xianShi) {
        StringBuilder sql = new StringBuilder("select * from ticket_open where 1=1");
        return db.query(sql.toString(),Ticket_open.class);
    }


    @Override
    public Page queryAllPage(Page page, XianShi xianShi) {
        StringBuilder sql = new StringBuilder("select {{columns}} from ticket_open where 1=1");
        List<Object> params = new ArrayList<>();

        if (xianShi.getNo() != null && xianShi.getNo().length() > 0) {
            sql.append(" and no like ?");
            params.add("%" + xianShi.getNo() + "%");
        }
        if (xianShi.getKaiDnaId() != 0) {
            sql.append(" and enterprise_id = ?");
            params.add(xianShi.getKaiDnaId());
        }
        if (xianShi.getShouDanId() != 0) {
            sql.append(" and acquire_enterprise_id = ?");
            params.add(xianShi.getShouDanId());
        }
        int pageIndex = page.getPageIndex();
        int pageSize = page.getPageSize();
        if (pageIndex == 0) {
            page.setPageIndex(1);
        }
        if (pageSize == 0) {
            page.setPageSize(3);
        }
        sql.append(" limit ?,? ");
        params.add((page.getPageIndex() - 1) * page.getPageSize());
        params.add(page.getPageSize());
        ArrayList<Ticket_open> datas = db.query(sql.toString().replace("{{columns}}", "*"), Ticket_open.class, params.toArray());
        ArrayList<XianShi> xianShiList = pojo(datas);
        page.setItems(xianShiList);

        int count = selectBookSize();
        page.setTotalItems(count);
        return page;
    }




    public Page queryAllPage1(Page page, XianShi xianShi) {
        StringBuilder sql = new StringBuilder("select {{columns}} from ticket_open where 1=1 and status = '开单中'");
        List<Object> params = new ArrayList<>();

        if (xianShi.getNo() != null && xianShi.getNo().length() > 0) {
            sql.append(" and no like ?");
            params.add("%" + xianShi.getNo() + "%");
        }
        if (xianShi.getKaiDnaId() != 0) {
            sql.append(" and enterprise_id = ?");
            params.add(xianShi.getKaiDnaId());
        }
        if (xianShi.getShouDanId() != 0) {
            sql.append(" and acquire_enterprise_id = ?");
            params.add(xianShi.getShouDanId());
        }
        int pageIndex = page.getPageIndex();
        int pageSize = page.getPageSize();
        if (pageIndex == 0) {
            page.setPageIndex(1);
        }
        if (pageSize == 0) {
            page.setPageSize(3);
        }
        sql.append(" limit ?,? ");
        params.add((page.getPageIndex() - 1) * page.getPageSize());
        params.add(page.getPageSize());
        ArrayList<Ticket_open> datas = db.query(sql.toString().replace("{{columns}}", "*"), Ticket_open.class, params.toArray());
        ArrayList<XianShi> xianShiList = pojo1(datas);
        page.setItems(xianShiList);

        int count = selectBookSize1();
        page.setTotalItems(count);
        return page;
    }






/*            sql = "select * from ticket_open limit ?,?";
            return db.query(sql,Ticket_open.class,(page.getPageIndex()-1)*page.getPageSize(),page.getPageSize());*/

    /**
     * 查询开单状态是“开单中”的所有数据
     *
     * @param status 开单状态
     * @return 返回展示所有状态是开单中的开单数据集合
     */
    @Override
    public ArrayList<Ticket_open> selectTicket_openByStatus(String status) {
        String sql = "select * from ticket_open where status=?";
        return db.query(sql, Ticket_open.class, status);
    }

    @Override
    public int selectBookSize1() {
        StringBuilder sql = new StringBuilder("select count(ticket_open_id) from ticket_open where status = '开单中'");
/*        if(book.getBookName()!=null){
            sql.append( "and BOOKNAME like ?");
            return db.query(sql.toString(),"%"+book.getBookName()+"%");
        }else if(book.getBookAuthor()!=null){
            sql.append( "and BOOKAUTHOR like ?");
            return db.query(sql.toString(),"%"+book.getBookAuthor()+"%");
        }else if(book.getBookPublish()!=null){
            sql.append( "and BOOKPUBLISH like ?");
            return db.query(sql.toString(),"%"+book.getBookPublish()+"%");
        }else{*/
        return db.query(sql.toString());
//        }
    }

    @Override
    public int selectBookSize() {
        StringBuilder sql = new StringBuilder("select count(ticket_open_id) from ticket_open");
/*        if(book.getBookName()!=null){
            sql.append( "and BOOKNAME like ?");
            return db.query(sql.toString(),"%"+book.getBookName()+"%");
        }else if(book.getBookAuthor()!=null){
            sql.append( "and BOOKAUTHOR like ?");
            return db.query(sql.toString(),"%"+book.getBookAuthor()+"%");
        }else if(book.getBookPublish()!=null){
            sql.append( "and BOOKPUBLISH like ?");
            return db.query(sql.toString(),"%"+book.getBookPublish()+"%");
        }else{*/
        return db.query(sql.toString());
//        }
    }


}
